package com.rs.controller;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import com.github.pagehelper.Page;
import com.rs.model.ZhLeanprogress;
import com.rs.model.ZhPosition;
import com.rs.service.ZhLeanprogressService;
import com.rs.util.SpringContextUtils;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class ZhLeanprogressList extends JInternalFrame {
	private JTable table;

	
	private ZhLeanprogressService zhLeanprogressService = SpringContextUtils.getBean(ZhLeanprogressService.class);
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ZhLeanprogressList frame = new ZhLeanprogressList(null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private Object [][]data=null;
	
	private String head[]=null;
	
	//所有的部门信息
	private Page<ZhLeanprogress> dataList;
	
	//首页
	private Integer pageNum =1;
	
	JDesktopPane desktopPane_1;
	
	//总页数
	private Integer pages;


	/**
	 * Create the frame.
	 */
	public ZhLeanprogressList(final JDesktopPane desktopPane_1) {
		this.desktopPane_1 = desktopPane_1;
			setClosable(true);
			setMaximizable(true);
			setIconifiable(true);
			setResizable(false);
			setTitle("部门职务列表");
			setBounds(100, 100, 935, 472);
			getContentPane().setLayout(null);
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setBounds(0, 67, 919, 276);
			getContentPane().add(scrollPane);
			
			table = new JTable();
			table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
			DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中
			tcr.setHorizontalAlignment(SwingConstants.CENTER);// 这句和上句作用一样
			table.setDefaultRenderer(Object.class, tcr);
			head=new String[] {
					"编号", "大纲主题", "大纲标题", "大纲内容", "进度（最高为100）", "课程类别", "课程等级"
			        };
			
			table.setModel(new DefaultTableModel(queryData(pageNum, null),head));
			scrollPane.setViewportView(table);
			table.addMouseListener(new MouseAdapter()
			{
				public void mouseClicked(MouseEvent e) {
					int row =table.rowAtPoint(e.getPoint());// 选中行
					ZhLeanprogress zhPosition = new ZhLeanprogress();
					zhPosition.setLpgId((int)table.getValueAt(row, 0));
					zhPosition.setLpgTheme((String)table.getValueAt(row, 1));
					zhPosition.setLpgTitle((String)table.getValueAt(row, 2));
					zhPosition.setLpgTxt((String)table.getValueAt(row, 3));
					zhPosition.setLpgJd((int)table.getValueAt(row, 4));
					doDefaultCloseAction();
					new ZhLeanprogressListUpdate(zhPosition,desktopPane_1).setVisible(true);
				}
			});
			
			JPanel panel = new JPanel();
			panel.setBounds(0, 0, 919, 68);
			getContentPane().add(panel);
			panel.setLayout(null);
			
			JLabel lblNewLabel = new JLabel("大纲主题：");
			lblNewLabel.setBounds(29, 13, 78, 30);
			panel.add(lblNewLabel);
			
			final JComboBox comboBox = new JComboBox();
			comboBox.setModel(new DefaultComboBoxModel(getDepartmentData()));
			comboBox.setBounds(105, 18, 105, 21);
			panel.add(comboBox);
			
			JLabel label = new JLabel("大纲标题：");
			label.setBounds(228, 18, 78, 21);
			panel.add(label);
			
			final JComboBox comboBox_1 = new JComboBox();
			comboBox_1.setModel(new DefaultComboBoxModel(getPositionData()));
			comboBox_1.setBounds(305, 18, 105, 21);
			panel.add(comboBox_1);
			
			JLabel label_1 = new JLabel("课程类型：");
			label_1.setBounds(444, 21, 78, 15);
			panel.add(label_1);
			
			final JComboBox comboBox_2 = new JComboBox();
			comboBox_2.setModel(new DefaultComboBoxModel(new String[] {"无", "java", "php"}));
			comboBox_2.setBounds(520, 18, 93, 21);
			panel.add(comboBox_2);
		
			
			JButton btnNewButton = new JButton("查询");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					ZhLeanprogress zhLeanprogress = new ZhLeanprogress();
					if(!comboBox.getSelectedItem().equals("无")){
						zhLeanprogress.setLpgTheme((String)comboBox.getSelectedItem());
					}
					if(!comboBox_1.getSelectedItem().equals("无")){
						zhLeanprogress.setLpgTitle((String)comboBox_1.getSelectedItem());
					}
					if(!comboBox_2.getSelectedItem().equals("无")){
						if(comboBox_2.getSelectedItem().equals("java")) {
							zhLeanprogress.setLpgType(0);
						}else {
							zhLeanprogress.setLpgType(1);
						}
					}
					SetTheValue(zhLeanprogress);
				}
			});
			btnNewButton.setBounds(733, 17, 93, 23);
			panel.add(btnNewButton);
			
		
			
			
			
			JPanel panel_1 = new JPanel();
			panel_1.setBounds(0, 341, 919, 102);
			getContentPane().add(panel_1);
			panel_1.setLayout(null);
			
			JButton btnNewButton_1 = new JButton("首页");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					pageNum=1;
					ZhLeanprogress zhLeanprogress = new ZhLeanprogress();
					if(!comboBox.getSelectedItem().equals("无")){
						zhLeanprogress.setLpgTheme((String)comboBox.getSelectedItem());
					}
					if(!comboBox_1.getSelectedItem().equals("无")){
						zhLeanprogress.setLpgTitle((String)comboBox_1.getSelectedItem());
					}
					if(!comboBox_2.getSelectedItem().equals("无")){
						if(comboBox_2.getSelectedItem().equals("java")) {
							zhLeanprogress.setLpgType(0);
						}else {
							zhLeanprogress.setLpgType(1);
						}
					}
					SetTheValue(zhLeanprogress);
				}
			});
			btnNewButton_1.setBounds(56, 51, 93, 23);
			panel_1.add(btnNewButton_1);
			
			JButton btnNewButton_2 = new JButton("上一页");
			btnNewButton_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					pageNum-=1;
					// 判断是否是第一页				
					if(pageNum < 1) {
						showMessageDialog("已是第一页！");
						pageNum=1;
					}else {
						ZhLeanprogress zhLeanprogress = new ZhLeanprogress();
						if(!comboBox.getSelectedItem().equals("无")){
							zhLeanprogress.setLpgTheme((String)comboBox.getSelectedItem());
						}
						if(!comboBox_1.getSelectedItem().equals("无")){
							zhLeanprogress.setLpgTitle((String)comboBox_1.getSelectedItem());
						}
						if(!comboBox_2.getSelectedItem().equals("无")){
							if(comboBox_2.getSelectedItem().equals("java")) {
								zhLeanprogress.setLpgType(0);
							}else {
								zhLeanprogress.setLpgType(1);
							}
						}
						SetTheValue(zhLeanprogress);
					}
				}
			});
			btnNewButton_2.setBounds(499, 51, 93, 23);
			panel_1.add(btnNewButton_2);
			
			JButton btnNewButton_3 = new JButton("下一页");
			btnNewButton_3.addActionListener(new ActionListener() {
				
				
				
				public void actionPerformed(ActionEvent e) {
					// 判断是否是最后一页				
					if(pageNum == pages) {
						showMessageDialog("已是最后一页！");
					}else {
						pageNum+=1;
						ZhLeanprogress zhLeanprogress = new ZhLeanprogress();
						if(!comboBox.getSelectedItem().equals("无")){
							zhLeanprogress.setLpgTheme((String)comboBox.getSelectedItem());
						}
						if(!comboBox_1.getSelectedItem().equals("无")){
							zhLeanprogress.setLpgTitle((String)comboBox_1.getSelectedItem());
						}
						if(!comboBox_2.getSelectedItem().equals("无")){
							if(comboBox_2.getSelectedItem().equals("java")) {
								zhLeanprogress.setLpgType(0);
							}else {
								zhLeanprogress.setLpgType(1);
							}
						}
						SetTheValue(zhLeanprogress);
					}
				}
			});
			btnNewButton_3.setBounds(778, 51, 93, 23);
			panel_1.add(btnNewButton_3);
		}
		
		//提示弹窗
		public void showMessageDialog(String massage){
			JOptionPane.showMessageDialog(this,massage);
		}
		
		//清空重新渲染数据
		public void SetTheValue(ZhLeanprogress record){
			TableModel tb=table.getModel();
			listNull(tb,10,3);
			table.setModel(new DefaultTableModel(queryData(pageNum,record),head));
		}
		
		//清空数据
		public void listNull(TableModel tb,int row,int cou){
			 for(int i=0;i<row;i++){
				for(int j=0;j<cou;j++){
					tb.setValueAt(null, i, j);
				}
			 }
		}
		
		 //生成表格数据
	    /**
	     * @return
	     */
	    public Object[][] queryData(Integer pageNum,ZhLeanprogress record){
	        List<ZhLeanprogress> list= getData(pageNum, record);
	        data = new Object[10][head.length];
	        for(int i=0;i<10;i++){
	            for(int j=0;j<head.length;j++){
	            	if(i>list.size()-1) {
	            		 data[i][0]=null;
	                     data[i][1]=null;
	                     data[i][2]=null;
	                     data[i][3]=null;
	                     data[i][4]=null;
	                     data[i][5]=null;
	                     data[i][6]=null;
	            	}else {
	            		 data[i][0]=list.get(i).getLpgId();
	                     data[i][1]=list.get(i).getLpgTheme();
	                     data[i][2]=list.get(i).getLpgTitle();
	                     data[i][3]=list.get(i).getLpgTxt();
	                     data[i][4]=list.get(i).getLpgJd();
	                     if (list.get(i).getLpgType()  == 0) {
	                    	  data[i][5]= "java";
						}else {
							  data[i][5]= "php";
						}
	                     if (list.get(i).getLpgLeve()  == 0) {
	                    	  data[i][6]= "初级";
						}else if(list.get(i).getLpgLeve()  == 1){
							  data[i][6]= "中级";
						}else {
							data[i][6]= "高级";
						}
	            	}
	            }
	        }
	        return data;
	    }
		//获取数据
		public List<ZhLeanprogress> getData(Integer pageNum,ZhLeanprogress record){
			
			 dataList =	zhLeanprogressService.slecteLeanprogressesAllLeanprogresses(pageNum,record);
			pages = dataList.getPages();
			System.out.println(pages);
			//获取数据 
			List<ZhLeanprogress> data =	new ArrayList<ZhLeanprogress>();
			for (ZhLeanprogress zhLeanprogress : dataList.getResult()) {
				data.add(zhLeanprogress);
			}
			return data;
		}
		
		//获取大纲主题
		public String[] getDepartmentData(){
			//获取数据 
			 LinkedHashSet<String> hs = new LinkedHashSet<String>();
			for (ZhLeanprogress zhLeanprogress : dataList.getResult()) {
				hs.add(zhLeanprogress.getLpgTheme());
			}
			String[] typeTeacherStrings =  new String[hs.size()+1];
			int i = 0;
			typeTeacherStrings[0] = "无";
			for (String string : hs) {
				i++;
					typeTeacherStrings[i] = string;
			}
			return typeTeacherStrings;
		}
		
		//获取大纲标题
		public String[] getPositionData(){
			//获取数据 
			 LinkedHashSet<String> hs = new LinkedHashSet<String>();
				for (ZhLeanprogress zhLeanprogress : dataList.getResult()) {
					hs.add(zhLeanprogress.getLpgTitle());
				}
				String[] typeTeacherStrings =  new String[hs.size()+1];
				int i = 0;
				typeTeacherStrings[0] = "无";
				for (String string : hs) {
					i++;
						typeTeacherStrings[i] = string;
				}
				return typeTeacherStrings;
		}
		
		
}
